package com.example.springbootsfzy.controller.sunnan;

import com.example.springbootsfzy.domain.PsychologicalKnowledge;
import com.example.springbootsfzy.domain.SelectedArticles;
import com.example.springbootsfzy.service.sunnan.PsychologicalKnowledgeService;
import com.example.springbootsfzy.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/psychologicalKnowledge")
public class PsychologicalKnowledgeController {
    @Autowired
    private PsychologicalKnowledgeService psychologicalKnowledgeService;

    /**
     * 分页查询所有心理知识
     * @param pageNum 当前页码
     * @param pageSize 每页显示的数量
     * @return PageInfo
     */
    @GetMapping
    @ResponseBody
    public PageInfo<PsychologicalKnowledge> getArticles(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return psychologicalKnowledgeService.getPsychologicalKnowledge(pageNum, pageSize);
    }

    /**
     * 根据consultantId查询心理知识
     * @param consultantId 咨询师id
     * @return List<PsychologicalKnowledge>
     */
    @GetMapping("/selectByConsultantId/{consultantId}")
    @ResponseBody
    public R selectByConsultantId(@PathVariable int consultantId) {
        return R.ok(psychologicalKnowledgeService.selectByConsultantId(consultantId)) ;
    }

    /**
     * 添加心理知识
     * @param psychologicalKnowledge 心理知识对象
     * @return R 响应结果
     */
    @PostMapping
    @ResponseBody
    public R addPsychologicalKnowledge(@RequestBody PsychologicalKnowledge psychologicalKnowledge) {
        if (psychologicalKnowledgeService.insert(psychologicalKnowledge) > 0) {
            return R.ok(null);
        } else {
            return R.fail("添加失败");
        }
    }

    /**
     * 根据id删除心理知识
     * @param knowledgeId 心理知识id
     * @return R 响应结果
     */
    @DeleteMapping("/{knowledgeId}")
    public R deleteById(@PathVariable int knowledgeId) {
        if (psychologicalKnowledgeService.deleteByPrimaryKey(knowledgeId) > 0) {
            return R.ok(null);
        } else {
            return R.fail("删除失败");
        }
    }

    /**
     * 根据id更新心理知识
     * @param psychologicalKnowledge 心理知识对象
     * @return R 响应结果
     */
    @PutMapping
    @ResponseBody
    public R updateById(@RequestBody PsychologicalKnowledge psychologicalKnowledge) {
        if (psychologicalKnowledgeService.updateByPrimaryKeySelective(psychologicalKnowledge) > 0) {
            return R.ok(null);
        } else {
            return R.fail("更新失败");
        }
    }
}
